package com.red.wood.dubbo.task;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.dubbo.service.IrcsBusinessDataService;

//@Service
public class IrcsBusinessDubboTask implements Serializable{
	
	private static final long serialVersionUID = 1L;

	private static final Log log = LogFactory.getLog(IrcsBusinessDubboTask.class);
	
	@Autowired
	private CommonDao dao;
	
	@Autowired
    private IrcsBusinessDataService service;
	
	public void doTask() {
		
		System.err.println("=====IRCS业务任务开始=======");
		
		getIrcsDataInfo();
		
		getIrcsHouseInfo();
		
		getIrcsIpInfo();
		
		getIrcsBandWidthInfo();
		
		getIrcsUserInfo();
		
		getIrcsDomainInfo();
		
		getIrcsPublicDomainInfo();
		
		getIrcsEmergencyContactInfo();
		
		getIrcsHouseOfficerInfo();
		
		getIrcsServiceInfo();
		
		System.err.println("=====IRCS业务任务结束=======");
	}
	
	private void getIrcsServiceInfo() {
		try {
			log.info("=============IRCS服务信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsServiceInfo(min, max);
				String sql = "insert into t_ircs_userinfo_serviceinfo_test(ircsId,userId,serviceId,serviceContent,regType,regId,setMode) values (?,?,?,?,?,?,?)";
				String columns = "IRCSID,USERID,SERVICEID,SERVICECONTENT,REGTYPE,REGID,SETMODE";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_userinfo_serviceinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS服务信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsHouseOfficerInfo() {
		try {
			log.info("=============IRCS安全责任人信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsHouseOfficerInfo(min, max);
				String sql = "insert into t_ircs_houseinfo_houseofficer_test(ircsId,houseId,houseOfficeName,houseOfficeIdType,houseOfficeId,houseOfficeTel,houseOfficeMobile,houseOfficeEmail) values (?,?,?,?,?,?,?,?)";
				String columns = "IRCSID,HOUSEID,HOUSEOFFICENAME,HOUSEOFFICEIDTYPE,HOUSEOFFICEID,HOUSEOFFICETEL,HOUSEOFFICEMOBILE,HOUSEOFFICEEMAIL";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_houseinfo_houseofficer_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS安全责任人信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsEmergencyContactInfo() {
		try {
			log.info("=============IRCS企业应急联系人信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsEmergencyContactInfo(min, max);
				String sql = "insert into t_ircs_emergencycontact_test(ircsId,name,idType,id,tel,mobile,email) values (?,?,?,?,?,?,?)";
				String columns = "IRCSID,NAME,IDTYPE,ID,TEL,MOBILE,EMAIL";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_emergencycontact_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS企业应急联系人信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsPublicDomainInfo() {
		try {
			log.info("=============IRCS公共域名信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsPublicDomainInfo(min, max);
				String sql = "insert into t_ircs_publicdomaininfo_test(ircsId,domainId,domainName,domainLevel) values (?,?,?,?)";
				String columns = "IRCSID,DOMAINID,DOMAINNAME,DOMAINLEVEL";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_publicdomaininfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS公共域名信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsDomainInfo() {
		try {
			log.info("=============IRCS域名信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsDomainInfo(min, max);
				String sql = "insert into t_ircs_userinfo_serviceinfo_domain_test(ircsId,userId,serviceId,domainId,domainName) values (?,?,?,?,?)";
				String columns = "IRCSID,USERID,SERVICEID,DOMAINID,DOMAINNAME";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_userinfo_serviceinfo_domain_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS域名信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsUserInfo() {
		try {
			log.info("=============IRCS用户信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsUserInfo(min, max);
				String sql = "insert into t_ircs_queryuserinfo_test(province,ircsId,userId,nature,unitName,idType,idNo,unitNature,officerName,officerMobile,officerTel,officerEmail,address,zipCode,registerTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
				String columns = "PROVINCE,IRCSID,USERID,NATURE,UNITNAME,IDTYPE,IDNO,UNITNATURE,OFFICERNAME,OFFICERMOBILE,OFFICERTEL,OFFICEREMAIL,ADDRESS,ZIPCODE,REGISTERTIME";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_queryuserinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS用户信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsBandWidthInfo() {
		try {
			log.info("=============IRCS链路信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsBandWidthInfo(min, max);
				String sql = "insert into t_ircs_gatewayinfo_test(province,ircsId,gatewayIp,bandWidth,linkType,accessUnit,internetAccess) values (?,?,?,?,?,?,?)";
				String columns = "PROVINCE,IRCSID,GATEWAYIP,BANDWIDTH,LINKTYPE,ACCESSUNIT,INTERNETACCESS";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_gatewayinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS链路信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsIpInfo() {
		try {
			log.info("=============IRCS IP信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsIpInfo(min, max);
				String sql = "insert into t_ircs_ipinfo_test(province,ircsId,startIp,endIp,type,sourceUnit,allocationUnit,allocationTime) values (?,?,?,?,?,?,?,?)";
				String columns = "PROVINCE,IRCSID,STARTIP,ENDIP,TYPE,SOURCEUNIT,ALLOCATIONUNIT,ALLOCATIONTIME";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_ipinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS IP信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsHouseInfo() {
		try {
			log.info("=============IRCS机房信息插入开始==============");
			int min = 0;
			int max = 10000;
			int pageSize = 10000;
			int totalCount = 0;
			int i = 0;
			do{
				//由于数据量比较大，一次性无法将所有数据传过来，故做分页处理，每次处理10000条数据
				List<Map<String,Object>> list = service.getIrcsHouseInfo(min, max);
				String sql = "insert into t_ircs_houseinfo_test(ircsId,houseId,houseName,houseType,houseProvince,houseCity,houseCounty,houseAdd,houseZip) values (?,?,?,?,?,?,?,?,?)";
				String columns = "IRCSID,HOUSEID,HOUSENAME,HOUSETYPE,HOUSEPROVINCE,HOUSECITY,HOUSECOUNTY,HOUSEADD,HOUSEZIP";
				if(i == 0 && list != null && list.size() > 0) {
					dao.deleteTableData("delete from t_ircs_houseinfo_test");
				}
				dao.insertBatchTableData(list, sql, columns,10000);
				log.info("当前插入的数据量："+max);
				i++;
				totalCount = list.size();
				min = max;
				max = max + pageSize;
			}while(pageSize == totalCount);
			log.info("============IRCS机房信息插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
	
	private void getIrcsDataInfo() {
		try {
			log.info("============IRCS企业信息插入开始==============");
			List<Map<String,Object>> list = service.getIrcsDataInfo();
			
			dao.deleteTableData("delete from t_ircs_ircsdata_test");
			String sql = "insert into t_ircs_ircsdata_test(province,ircsId,ircsName,ircsAdd,corp) values (?,?,?,?,?)";
			String columns = "PROVINCE,IRCSID,IRCSNAME,IRCSADD,CORP";
			dao.insertBatchTableData(list, sql, columns,10000);
			log.info("============IRCS企业信息插入插入完成==============");
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
	}
}
